//
//  CacheSlider.swift
//  funcode
//
//  Created by wanjiading on 2017/8/16.
//  Copyright © 2017年 WanJiading. All rights reserved.
//

import UIKit

class CacheSlider: UISlider {
    
    private lazy var cachedView: UIProgressView = {
        let cachedView = UIProgressView()
        cachedView.trackTintColor = UIColor.clear
        cachedView.tintColor = UIColor.clear
        //        view.isHidden = true
        return cachedView
    }()
    private var didAddedToSuperView = false
    
    //    /// 进度最大值
    //    var maxValue: Float = 0.0 {
    //        didSet {
    //            self.maximumValue = maxValue
    //        }
    //    }
    //
    //    /// 进度最小值
    //    var minValue: Float = 0.0 {
    //        didSet {
    //            self.minimumValue = minValue
    //        }
    //    }
    //
    //    /// 播放进度
    //    var playedValue: Float = 0.0 {
    //        didSet {
    //            self.value = playedValue
    //        }
    //    }
    
    /// 缓冲进度
    var cachedValue: Float = 0.0 {
        didSet {
            cachedView.setProgress(cachedValue / maximumValue, animated: false)
        }
    }
    
    /// 已播放的颜色
    var playedColor: UIColor = UIColor.rgb("9bc34e") {
        didSet {
            self.minimumTrackTintColor = playedColor
        }
    }
    
    /// 已缓存的颜色
    var cachedColor: UIColor = UIColor.rgb("ffffff") {
        didSet {
            cachedView.progressTintColor = cachedColor
        }
    }
    
    /// 未加载的颜色
    var noPlayedColor: UIColor = UIColor.rgb("ffffff", alpha: 0.3) {
        didSet {
            cachedView.backgroundColor = noPlayedColor
        }
    }
    
    override func awakeFromNib() {
        super.awakeFromNib()
        
        self.maximumTrackTintColor = UIColor.clear
        self.tintColor = UIColor.clear
    }
    
    override func layoutSubviews() {
        super.layoutSubviews()
        
        for v in self.subviews {
            if v.isKind(of: UIView.self) && !v.isKind(of: UIImageView.self) {
                if (!didAddedToSuperView) {
                    didAddedToSuperView = true
                    cachedView = UIProgressView()
                    self.insertSubview(cachedView, at: 0)
                }
                let x: CGFloat = 2
                cachedView.frame = CGRect(x: x, y: v.y, width: self.width - x * 2, height: v.height)
            }
        }
    }
}
